package com.telchina.workorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.utils.DateUtils;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.telchina.workorder.mapper.WoReturnVisitMapper;
import com.telchina.workorder.domain.WoReturnVisit;
import com.telchina.workorder.service.IWoReturnVisitService;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 回访单Service业务层处理
 *
 * @author konghao
 * @date 2023-02-21
 */
@Service
public class WoReturnVisitServiceImpl extends ServiceImpl<WoReturnVisitMapper, WoReturnVisit> implements IWoReturnVisitService {

    /**
     * 查询回访单
     *
     * @param id 回访单ID
     * @return 回访单
     */
    @Override
    public WoReturnVisit selectById(String id) {
        return baseMapper.selectById(id);
    }

    /**
     * 分页查询回访单列表
     *
     * @param woReturnVisit 回访单
     * @return 回访单
     */
    @Override
    public IPage<WoReturnVisit> selectList(IPage<WoReturnVisit> page, WoReturnVisit woReturnVisit) {
        return baseMapper.selectWoReturnVisitPage(page, woReturnVisit);
    }

    /**
     * 查询所有回访单列表
     *
     * @param woReturnVisit 回访单
     * @return 回访单
     */
    @Override
    public List<WoReturnVisit> selectListAll(WoReturnVisit woReturnVisit) {
        return baseMapper.selectWoReturnVisitList(woReturnVisit);
    }

    /**
     * 新增回访单
     *
     * @param woReturnVisit 回访单
     * @return 结果
     */
    @Override
    public AjaxResult insert(WoReturnVisit woReturnVisit) {
        woReturnVisit.setReturnVisitNo(getReturnVisitNo());// 更新编号
        woReturnVisit.setFilledTime(new Date()); // 更新登记时间
        return saveOrUpdate(woReturnVisit) ? AjaxResult.success(woReturnVisit) : AjaxResult.error();
    }

    /**
     * 修改回访单
     *
     * @param woReturnVisit 回访单
     * @return 结果
     */
    @Override
    public int update(WoReturnVisit woReturnVisit) {
        woReturnVisit.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(woReturnVisit);
    }

    /**
     * 批量删除回访单
     *
     * @param ids 需要删除的回访单ID
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除回访单信息
     *
     * @param id 回访单ID
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return baseMapper.deleteById(id);
    }

    /**
     * 根据工单id获取最新的一条回访单
     *
     * @param workOrderNumber
     * @return
     */
    @Override
    public WoReturnVisit selectNewOneByWorkOrderNumber(String workOrderNumber) {
        IPage<WoReturnVisit> woReturnVisitPage = baseMapper.selectPage(new Page<WoReturnVisit>(1, 1),
                new QueryWrapper<WoReturnVisit>().lambda().eq(WoReturnVisit::getWorkOrderNumber, workOrderNumber).orderByDesc(WoReturnVisit::getWorkOrderNumber));
        return woReturnVisitPage.getRecords().size() > 0 ? woReturnVisitPage.getRecords().get(0) : null;
    }

    /**
     * 同步方法生成工单编号
     *
     * @return
     */
    @Synchronized
    private String getReturnVisitNo() {
        String paramsDate = DateUtils.parseDateToStr("yyMMdd", DateUtils.getNowDate());
        // 获取当日最大单号
        WoReturnVisit woReturnVisit = baseMapper.selectOne(new QueryWrapper<WoReturnVisit>().select("max(return_visit_no) return_visit_no")
                .likeRight("return_visit_no", paramsDate));
        if (woReturnVisit != null) {
            return String.valueOf(new BigDecimal(woReturnVisit.getReturnVisitNo()).add(BigDecimal.ONE));
        } else {
            return paramsDate + "00001";
        }
    }
}
